// @author U099151W Sun Chenchen
// all functions in this file  are implemented by the author unless specifically indicated
var Camera = function() {
    var _sx, _sy, _w, _h;
    var _marginLeft = 2;
    var _marginRight = 2;
    var _marginTop = 2;
    var _marginBottom = 2;

    this.updateCameraBasedOnPlayer = function(x, y) {
        if (x > _sx + _marginLeft && x < _sx + _w  - _marginRight &&
            y > _sy + _marginTop && y < _sy + _h  - _marginBottom) {
        } else {
            var leftOffset = (_sx + _marginLeft) - x;
            var rightOffset = x + 1 - (_sx + _w  - _marginRight);
            var topOffset = (_sy + _marginTop) - y;
            var bottomOffset = y + 1  - (_sy + _h  - _marginBottom);
            if (leftOffset > 0) {
                _sx = _sx - leftOffset > 0 ? _sx - leftOffset : 0;
            }
            if (rightOffset > 0) {
                _sx += rightOffset;
            }
            if (topOffset > 0) {
                _sy = _sy - topOffset > 0 ? _sy - topOffset : 0;
            }
            if (bottomOffset > 0) {
                _sy += bottomOffset;
            }
            map.updateGridsLevel(camera.getCameraParameter());
        }
    };

    this.updateCameraStart = function(x, y) {
        _sx = x;
        _sy = y;
    };

    this.updateCameraViewSize = function(w, h) {
        _w = w;
        _h = h;
    };

    this.getCameraParameter = function() {
        return {
            sx : _sx,
            sy : _sy,
            w : _w,
            h : _h
        };
    };

    this.init = function(args) {
        _sx = args.sx;
        _sy = args.sy;
        _w = args.w;
        _h = args.h;
    };

};
var camera = new Camera();